package com.ruoyi.shop.goods.service.impl;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.shop.goods.domain.ShopBrand;
import com.ruoyi.shop.goods.domain.ShopCategoryBrand;
import com.ruoyi.shop.goods.mapper.ShopBrandMapper;
import com.ruoyi.shop.goods.mapper.ShopCategoryBrandMapper;
import com.ruoyi.shop.goods.service.IShopBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * brandService业务层处理
 * 
 * @author ruoyi
 * @date 2022-02-17
 */
@Service
public class ShopBrandServiceImpl implements IShopBrandService {
	@Autowired
	private ShopBrandMapper shopBrandMapper;
	@Autowired
	private ShopCategoryBrandMapper shopCategoryBrandMapper;

	/**
	 * 查询brand
	 * 
	 * @param brandId 品牌主键
	 * @return brand
	 */
	@Override
	public ShopBrand selectBrandById(Long brandId) {
		return shopBrandMapper.selectBrandById(brandId);
	}

	/**
	 * 查询品牌列表
	 * 
	 * @param brand
	 * @return 品牌集合
	 */
	@Override
	public List<ShopBrand> selectBrandList(ShopBrand brand) {
		return shopBrandMapper.selectBrandList(brand);
	}

	/**
	 * 新增品牌
	 * 
	 * @param shopBrand 品牌
	 * @return 结果
	 */
	@Override
	public int insertBrand(ShopBrand brand) {
		brand.setCreateTime(DateUtils.getNowDate());
		return shopBrandMapper.insertBrand(brand);
	}

	/**
	 * 修改品牌
	 * 
	 * @param shopBrand 品牌
	 * @return 结果
	 */
	@Override
	public int updateBrand(ShopBrand brand) {
		brand.setUpdateTime(DateUtils.getNowDate());
		return shopBrandMapper.updateBrand(brand);
	}

	/**
	 * 删除品牌
	 * 
	 * @param brandId 品牌主键
	 * @return 结果
	 */
	@Override
	public int deleteBrandById(Long brandId) {
		return shopBrandMapper.deleteBrandById(brandId);
	}

	/**
	 * 批量删除品牌
	 * 
	 * @param ids 品牌主键集合
	 * @return 结果
	 */
	@Override
	public int deleteBrandByIds(String ids) {
		return shopBrandMapper.deleteBrandByIds(Convert.toLongArray(ids));
	}

	/**
	 * 查询可用品牌列表
	 * 
	 * @return 品牌集合
	 */
	@Override
	public List<ShopBrand> queryBrands() {
		return shopBrandMapper.queryBrands();
	}
	
	/**
	 * 查询所有品牌列表-标识已选中的
	 * 
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<ShopBrand> selectBrandByCategoryId(Long categoryId) {
		List<ShopBrand> categoryBrands = shopBrandMapper.selectBrandByCategoryId(categoryId);
		List<ShopBrand> brands = shopBrandMapper.selectBrandList(null);
		for (ShopBrand brand : brands) {
			for (ShopBrand categoryBrand : categoryBrands) {
				if (brand.getBrandId().longValue() == categoryBrand.getBrandId().longValue()) {
					brand.setFlag(true);
					break;
				}
			}
		}
		return brands;
	}
	
	/**
	 * 查询分类管理关联的品牌列表
	 * 
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<ShopBrand> queryBrandByCategoryId(Long categoryId) {
		List<ShopBrand> brands = shopBrandMapper.selectBrandByCategoryId(categoryId);
		return brands;
	}
	
	/**
	 * 保存分类品牌关联
	 * 
	 * @param brandIds
	 * @param categoryId
	 * @return
	 */
	@Transactional
	public int saveCategoryBrand(String brandIds, Long categoryId) {
		Long[] BrandIdArr = Convert.toLongArray(brandIds);
		shopCategoryBrandMapper.deleteByCategoryId(categoryId);
		for (Long brandId : BrandIdArr) {
			ShopCategoryBrand categoryBrand = new ShopCategoryBrand();
			categoryBrand.setBrandId(brandId);
			categoryBrand.setCategoryId(categoryId);
			categoryBrand.setCreateTime(DateUtils.getNowDate());
			shopCategoryBrandMapper.insertCategoryBrand(categoryBrand);
		}
		return 1;
	}
}
